package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.BizProductSeries;
import com.ruoyi.system.domain.vo.BizProductSeriesRequest;
import com.ruoyi.system.domain.vo.BizProductSeriesResponse;
import com.ruoyi.system.mapper.BizProductSeriesMapper;
import com.ruoyi.system.service.BizProductSeriesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BizProductSeriesServiceImpl implements BizProductSeriesService {

    @Autowired
    private BizProductSeriesMapper productSeriesMapper;

    @Override
    public List<BizProductSeriesResponse> selectProductSeriesList(BizProductSeriesRequest productSeriesRequest) {
        List<BizProductSeries> productSeriesList = productSeriesMapper.selectProductSeriesList(productSeriesRequest);
        if (productSeriesList != null && !productSeriesList.isEmpty()) {
            List<BizProductSeriesResponse> productSeriesResponseList = new ArrayList<>();
            for (BizProductSeries productSeries : productSeriesList) {
                BizProductSeriesResponse productSeriesResponse = new BizProductSeriesResponse();
                BizProductSeries bizProductSeries = productSeriesMapper.selectProductSeriesById(productSeries.getParentId());
                BeanUtils.copyProperties(productSeries, productSeriesResponse);
                if (bizProductSeries != null) {
                    productSeriesResponse.setParentName(bizProductSeries.getName());
                }
                // 查询子系列列表
                BizProductSeriesRequest childProductSeriesRequest = new BizProductSeriesRequest();
                childProductSeriesRequest.setLevel(2);
                childProductSeriesRequest.setParentId(productSeries.getId());
                productSeriesResponse.setSeriesList(productSeriesMapper.selectProductSeriesList(childProductSeriesRequest));

                productSeriesResponseList.add(productSeriesResponse);
            }
            return productSeriesResponseList;
        }
        return new ArrayList<>();
    }

    @Override
    public BizProductSeriesResponse selectProductSeriesById(Long id) {
        BizProductSeries bizProductSeries = productSeriesMapper.selectProductSeriesById(id);
        if (null == bizProductSeries) {
            return null;
        }
        BizProductSeriesResponse productSeriesResponse = new BizProductSeriesResponse();
        BeanUtils.copyProperties(bizProductSeries, productSeriesResponse);
        BizProductSeries parentProductSeries = productSeriesMapper.selectProductSeriesById(bizProductSeries.getParentId());
        if (parentProductSeries != null) {
            productSeriesResponse.setParentName(parentProductSeries.getName());
        }
        // 查询子系列列表
        BizProductSeriesRequest childProductSeriesRequest = new BizProductSeriesRequest();
        childProductSeriesRequest.setLevel(2);
        childProductSeriesRequest.setParentId(bizProductSeries.getId());
        productSeriesResponse.setSeriesList(productSeriesMapper.selectProductSeriesList(childProductSeriesRequest));
        return productSeriesResponse;
    }

    @Override
    public int insertProductSeries(BizProductSeries productSeries) {
        return productSeriesMapper.insertProductSeries(productSeries);
    }

    @Override
    public int updateProductSeries(BizProductSeries productSeries) {
        return productSeriesMapper.updateProductSeries(productSeries);
    }

    @Override
    public int deleteProductSeriesById(Long id) {
        return productSeriesMapper.deleteProductSeriesById(id);
    }

    @Override
    public int checkProductSeriesNameUnique(String name, Long id) {
        return productSeriesMapper.checkProductSeriesNameUnique(name, id);
    }
}
